package com.luo.gmall.index.controller;

import com.luo.gmall.common.bean.ResponseVo;
import com.luo.gmall.index.service.IndexService;
import com.luo.gmall.pms.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Zoro
 * @date 2021年11月13
 */
@Controller
public class IndexController {

    @Autowired
    private IndexService indexService;

    @GetMapping("/")
    public String toIndex(Model model) {
        // 获取一级菜单的数据
        List<CategoryEntity> categoryEntities = indexService.queryLevelOneCategories();
        model.addAttribute("categories", categoryEntities);
        return "index";
    }

    @ResponseBody
    @GetMapping("index/cates/{parentId}")
    public ResponseVo<List<CategoryEntity>> queryListCategoryByParentId(@PathVariable Long parentId) {

        List<CategoryEntity> categoryEntities = indexService.queryListCategoryByParentId(parentId);
        return ResponseVo.ok(categoryEntities);
    }

    @ResponseBody
    @GetMapping("/index/testLock")
    public ResponseVo<String> testLock() {
        indexService.testLock();
        return ResponseVo.ok();
    }

    @ResponseBody
    @GetMapping("/index/testReader")
    public ResponseVo<String> testReader(){
        indexService.testReader();
        return ResponseVo.ok("读取成功！");
    }

    @ResponseBody
    @GetMapping("/index/testWriter")
    public ResponseVo<String> testWriter(){
        indexService.testWriter();
        return ResponseVo.ok("写入成功！");
    }


    @ResponseBody
    @GetMapping("/index/testLatch")
    public ResponseVo<String> testLatch(){
        indexService.testLatch();
        return ResponseVo.ok("班长锁门");
    }

    @ResponseBody
    @GetMapping("/index/testDown")
    public ResponseVo<String> testDown(){
        indexService.testDown();
        return ResponseVo.ok("一个同学出来了");
    }
}
